# database/__init__.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from .base import Base
from app.database.config import DB_URL

# 创建带连接池的引擎
engine = create_engine(
    DB_URL,
    pool_size=5,
    max_overflow=10,
    echo=True  # 开发环境显示SQL
)

# 初始化表结构
def init_db():
    Base.metadata.create_all(bind=engine)

# 创建线程安全的Session工厂
SessionLocal = sessionmaker(
    bind=engine,
    autoflush=False,
    autocommit=False,
    expire_on_commit=False
)